package com.xiaoshuidi.cloud.module.rooms.mq.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.pricing.vo.HousePricingTemplateVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomel.vo.RoomelUpdateReqVO;
import com.xiaoshuidi.cloud.module.rooms.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.rooms.enums.PricingRecordsType;
import com.xiaoshuidi.cloud.module.rooms.mq.producer.RoomEsProducer;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomPricingRecords;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomPricingRecordsRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Feng
 * @date 2023/5/17
 */
@Slf4j
@AllArgsConstructor
public class HousePricingListener extends AbstractExcelListener<HousePricingTemplateVo> {

    private final RoomHouseRepository roomHouseRepository;
    private final RoomEsProducer roomEsProducer;
    private final RoomPricingRecordsRepository pricingRecordsRepository;
    private Long apartmentId;
    private Boolean isConcentrated;

    @Override
    public void saveData() {
        if (CollectionUtil.isEmpty(this.cachedDataList)) {
            return;
        }
        List<String> noList = this.cachedDataList.stream().map(s -> s.getNo()).collect(Collectors.toList());
        List<RoomHouse> roomHouseList = roomHouseRepository.listByNos(apartmentId, isConcentrated, noList);
        Map<String, HousePricingTemplateVo> map = this.cachedDataList.stream().collect(Collectors.toMap(HousePricingTemplateVo::getNo, Function.identity()));
        List<RoomPricingRecords> recordsList = new ArrayList<>();
        List<RoomelUpdateReqVO> updateReqVOList = new ArrayList<>();
        roomHouseList.forEach(s -> {
            HousePricingTemplateVo vo = map.get(s.getNo());
            Double beforeMonthRental = s.getMonthRental();
            Double afterMonthRental = vo.getMonthRental();
            //如果价格为空，直接当做成功处理
            if (afterMonthRental == null) {
                map.remove(s.getNo());
                return;
            }
            if (afterMonthRental < 1 || afterMonthRental > 100000) {
                return;
            }
            s.setMonthRental(afterMonthRental);
            //ES保存数据
            RoomelUpdateReqVO roomelUpdateReqVO = new RoomelUpdateReqVO();
            roomelUpdateReqVO.setPrice(afterMonthRental);
            roomelUpdateReqVO.setId(s.getId());
            updateReqVOList.add(roomelUpdateReqVO);

            RoomPricingRecords houseRecords = setRoomPricingRecords(PricingRecordsType.HOUSE.getCode(), beforeMonthRental, afterMonthRental, s.getId());
            recordsList.add(houseRecords);
            map.remove(s.getNo());
        });
        roomHouseRepository.updateBatchById(roomHouseList);
        pricingRecordsRepository.saveBatch(recordsList);
        // 更新ES房源数据
        roomEsProducer.updateBatchRoomEl(updateReqVOList);
        //如果map不为空则部分成功
        if (CollectionUtil.isNotEmpty(map)) {
            String msg = map.values().stream().map(s -> s.getHouseNo()).collect(Collectors.joining("、"));
            msg = String.format("%s: %s;设置失败！", ErrorCodeConstants.HOUSE_PRICING_IMPORT_PARTIAL_SUCCESS.getMsg(), msg);
            throw new ServiceException(ErrorCodeConstants.HOUSE_PRICING_IMPORT_PARTIAL_SUCCESS.getCode(), msg);
        }
    }

    public RoomPricingRecords setRoomPricingRecords(String type, Double beforeMonthRental, Double afterMonthRental, Long mappingId) {
        RoomPricingRecords roomPricingRecords = new RoomPricingRecords();
        roomPricingRecords.setType(type)
                .setBeforeMonthRental(beforeMonthRental)
                .setAfterMonthRental(afterMonthRental)
                .setMappingId(mappingId);
        return roomPricingRecords;
    }
}
